﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using Algorithms.Tools;

/*
 * TH07 Groep 10
 * ----------------------------------------------------------------------------------
 * Versie   Door        Commentaar
 * 0.1      Mike        Opzet en programmeer werk
 * 0.2      Luca        Optimalisatie code
 *                      Hernoemen variabelen en functies
 *                      overbodige code verwijderd 
 *                      crop image naar eigen tool classe verplaatst.
 *               
 */

namespace THO7AlgoritmTimerApplication
{

    /// <summary>
    /// This class represent different features of a char. 
    /// Like width/height ratio, amount of white pixels vs black pixels, etc.
    /// </summary>
    public class CharFeature
    {
        #region private variables

        private char character;
        private VisionImage charImage;

        private int hwRatio;

        private double rightPercentPixels;
        private double topPercentPixels;

        #endregion

        #region public setters & getters

        public int HwRatio
        {
            get { return hwRatio; }
            set { hwRatio = value; }
        }

        public double TopPercentPixels
        {
            get { return topPercentPixels; }
            set { topPercentPixels = value; }
        }

        public double RightPercentPixels
        {
            get { return rightPercentPixels; }
            set { rightPercentPixels = value; }
        }
        #endregion

        #region constructors
        /// <summary>
        /// 
        /// </summary>
        /// <param name="charImage"></param>
        public CharFeature(VisionImage charImage)
        {
            this.charImage = charImage;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="character"></param>
        /// <param name="charImage"></param>
        public CharFeature(char character, VisionImage charImage)
        {
            this.character = character;
            this.charImage = charImage;
        }
        #endregion

        #region private functions
        /// <summary>
        /// 
        /// </summary>
        /// <param name="curPixels"></param>
        /// <param name="totalPixels"></param>
        /// <returns></returns>
        private double CalculatePixelPercentage(int curPixels, int totalPixels)
        {
            return (double)curPixels / totalPixels * 100.0;
        }

        #endregion

        #region public functions

        /// <summary>
        /// 
        /// </summary>
        public void CalculateFeatures()
        {

            charImage.Lock();
            int height = charImage.Height;
            int halfHeight = height / 2;
            int width = charImage.Width;
            int halfWidth = width / 2;
            int offset = charImage.Data.Stride;

            //needed for aspect ratio, Not Image but actual char(black pixels)
            int firstHorizontalBlackPixel = -1;
            int lastHorizontalBlackPixel = -1;

            int firstVerticalBlackPixel = -1;
            int lastVerticalBlackPixel = -1;

            // Percent Pixels (Top And Right)
            int totalBlackPixels = 0;
            int topPixels = 0;
            int rightPixels = 0;

            unsafe
            {
                byte* charPointer = (byte*)charImage.Data.Scan0;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        //Checks if pixel = black!
                        if (charPointer[0] == 0 && charPointer[1] == 0 && charPointer[2] == 0)
                        {
                            //Set first and last horizontal black pixel
                            if (firstHorizontalBlackPixel == -1)
                            {
                                firstHorizontalBlackPixel = height;
                            }
                            lastHorizontalBlackPixel = height;

                            //set first and last vertical black pixel
                            if (firstVerticalBlackPixel == -1)
                            {
                                firstVerticalBlackPixel = width;
                            }
                            else if (firstVerticalBlackPixel > width)
                            {
                                firstVerticalBlackPixel = width;
                            }

                            if (lastVerticalBlackPixel < width)
                            {
                                lastVerticalBlackPixel = width;
                            }

                            //Calcuate top part Pixels
                            if (y < halfHeight)
                            {
                                topPixels++;
                            }

                            //Calculate right part Pixels
                            if (x > halfWidth)
                            {
                                rightPixels++;
                            }
                            //Incrment blackpixel counter
                            totalBlackPixels++;
                        }
                        charPointer += 3;
                    }
                    charPointer += offset;
                }
            }
            charImage.Unlock();

            topPercentPixels = CalculatePixelPercentage(topPixels, totalBlackPixels);
            rightPercentPixels = CalculatePixelPercentage(rightPixels, totalBlackPixels);
        }
        #endregion




    }
}
